package com.example.mybatisplusdemo.service.comparison;

import com.example.mybatisplusdemo.mapper.FlightMapper;
import com.example.mybatisplusdemo.model.domain.Flight;
import com.example.mybatisplusdemo.model.dto.PriceComparisonDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class PriceComparisonService {

    private final FlightMapper flightMapper;

    // 供应商信息配置
    private static final Map<String, SupplierConfig> SUPPLIER_CONFIG = Map.of(
            "携程", new SupplierConfig(0.95, "新用户立减50", "起飞前2小时免费退改", 4.8),
            "去哪", new SupplierConfig(0.92, "会员专享9折", "起飞前24小时免费退改", 4.6),
            "飞猪", new SupplierConfig(0.90, "机票+酒店套餐优惠", "按航司规定执行", 4.7),
            "美团", new SupplierConfig(0.88, "支付立减30", "不可退改", 4.5)
    );

    /**
            * 获取航班比价信息
     */
    public List<PriceComparisonDTO> comparePrices(Long flightId) {
        // 1. 获取基础航班信息
        Flight baseFlight = flightMapper.selectById(flightId);
        if (baseFlight == null) {
            return Collections.emptyList();
        }

        // 2. 为每个供应商生成比价信息
        return SUPPLIER_CONFIG.entrySet().stream()
                .map(entry -> createComparisonDTO(baseFlight, entry.getKey(), entry.getValue()))
                .collect(Collectors.toList());
    }

    /**
            * 创建比价DTO
     */
    private PriceComparisonDTO createComparisonDTO(Flight flight, String supplier, SupplierConfig config) {
        PriceComparisonDTO dto = new PriceComparisonDTO();

        // 基础航班信息
        dto.setFlightId(flight.getId());
        dto.setFlightNumber(flight.getFlightNumber());
        dto.setDepartureAirportCode(flight.getDepartureAirportCode());
        dto.setArrivalAirportCode(flight.getArrivalAirportCode());
        dto.setDepartureTime(flight.getDepartureTime());
        dto.setArrivalTime(flight.getArrivalTime());
        dto.setAirlineCode(flight.getAirlineCode());
        dto.setDuration(flight.getDuration());
        dto.setBasePrice(flight.getPrice());

        // 供应商特定信息
        dto.setSupplier(supplier);
        dto.setSupplierPrice(calculateSupplierPrice(flight.getPrice(), config.priceModifier));
        dto.setDiscountInfo(config.discountInfo);
        dto.setRefundPolicy(config.refundPolicy);
        dto.setSupplierRating(config.userRating);
        dto.setBookingLink(generateBookingLink(flight.getFlightNumber(), supplier));

        // 其他信息
        dto.setServiceClass(flight.getServiceClass());
        dto.setWifiAvailable(flight.getWifiAvailable());
        dto.setPowerUsb(flight.getPowerUsb());
        dto.setMeals(flight.getMeals());

        return dto;
    }

    /**
            * 计算供应商价格
     */
    private BigDecimal calculateSupplierPrice(BigDecimal basePrice, double modifier) {
        return basePrice.multiply(BigDecimal.valueOf(modifier))
                .setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
            * 生成预订链接
     */
    private String generateBookingLink(String flightNumber, String supplier) {
        String domain = "";
        switch (supplier) {
            case "携程": domain = "https://ctrip.com"; break;
            case "去哪": domain = "https://qunar.com"; break;
            case "飞猪": domain = "https://fliggy.com"; break;
            case "美团": domain = "https://meituan.com"; break;
        }
        return domain + "/flight/" + flightNumber;
    }

    /**
            * 供应商配置内部类
     */
    private static class SupplierConfig {
        final double priceModifier;
        final String discountInfo;
        final String refundPolicy;
        final BigDecimal userRating;

        SupplierConfig(double priceModifier, String discountInfo,
                       String refundPolicy, double userRating) {
            this.priceModifier = priceModifier;
            this.discountInfo = discountInfo;
            this.refundPolicy = refundPolicy;
            this.userRating = BigDecimal.valueOf(userRating);
        }
    }
}